package sourceread;

import java.util.AbstractList;
import java.util.Arrays;
import java.util.List;
import java.util.RandomAccess;

public class MyArraylist <E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable{
    private static Object[] ELEMENTDATA_EMPTY = {};
    transient Object[] elementData = ELEMENTDATA_EMPTY;
    private int size;

    @Override
    public E get(int index) {
        return null;
    }

    @Override
    public int size() {
        return size;
    }

    public boolean add(E e){
        ensureCapacityInternal(size + 1);
        elementData[size++] = e;
        return true;
    }

    /**
     * 负责初始化默认值仓库；扩容仓库
     * @param minCapacity
     */
    private void ensureCapacityInternal(int minCapacity){
        if(ELEMENTDATA_EMPTY == elementData){
            minCapacity = 10;
        }
        if(minCapacity - elementData.length > 0){ //需要扩容
            grow(minCapacity);
        }
    }
    private void grow(int minCapacity){
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1); //每次扩展容量为源容量的一半
        if(newCapacity - minCapacity < 0){
            newCapacity = minCapacity;
        }
        elementData = Arrays.copyOf(elementData,newCapacity);
    }


}
